<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\LogType;
use app\common\model\MoneyLog;
use app\common\model\Wallet;
use app\common\model\WalletToken;
use fast\Tree;
use think\Db;

/**
 * 盲盒管理
 *
 * @icon   fa fa-list
 */
class Finance extends Backend
{

    /**
     * @var \app\common\model\MoneyLog
     */
    protected $model = null;
    protected $categorylist = [];
    protected $noNeedRight = ['selectpage'];
    protected $withdraw_status = [0 => '待审核', 1 => '已审核', 2 => '已完成', 3 => '已驳回'];

    public function _initialize()
    {
        $this->model = model('MoneyLog');
        parent::_initialize();
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $from_id = input('from_id', 0, 'intval');
            if ($from_id > 0) {
                $where = ['from_id' => $from_id, 'from' => input('from', '', 'trim')];
            }

            $list = $this->model->alias('f')
                ->join('user u', 'u.id=f.user_id', 'left')
                ->join('log_type l', 'l.mnemonic=f.from', 'left')
                ->where($where)
                ->field('f.*,u.username,l.name')
                ->order($sort, $order)
                ->paginate($limit);
            $rows = $list->items();
            $result = array("total" => $list->total(), "rows" => $rows);

            return json($result);
        }
        $from_id = $this->request->request('from_id');
        $this->view->assign('from_id', $from_id);
        $from = $this->request->request('from');
        $this->view->assign('from', $from);

        return $this->view->fetch();
    }

    /**
     * Selectpage搜索
     *
     * @internal
     */
    public function selectpage()
    {
        return parent::selectpage();
    }

    /**
     * 提现列表
    */
    public function withdraw()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = model('BillOrder')->alias('o')
                ->join('user u', 'u.id=o.uid', 'left')
                ->join('wallet_token wt', 'o.coin_id=wt.id', 'left')
                ->field('o.*,u.username,wt.title_en as coin_name')
                ->where(['o.type' => LogType::get_type_id('withdraw_coin')])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            $rows = $list->items();
            foreach ($rows as &$row) {
                foreach ($this->withdraw_status as $key => $value) {
                    if ($key == $row['status']) {
                        $row['status_str'] = $value;
                    }
                }
            }
            $result = array("total" => $list->total(), "rows" => $rows);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 驳回提现
    */
    public function reject()
    {
        $ids = input('ids', 0, 'intval');
        if ($ids) {
            $res = Wallet::reject($ids);
            if ($res['error'] > 0) {
                return $this->error($res['msg']);
            } else {
                return $this->success('驳回成功');
            }
        } else {
            return $this->error('订单号错误');
        }
    }

    /**
     * 提现审核通过
     */
    public function approved()
    {
        $ids = input('ids', 0, 'intval');
        if ($ids) {
            $res = Wallet::approved($ids);
            if ($res['error'] > 0) {
                return $this->error($res['msg']);
            } else {
                return $this->success('审核成功');
            }
        } else {
            return $this->error('订单号错误');
        }
    }
}
